<template>
  <div class="app-container">
    <t-form
        v-show="showSearch"
        ref="queryRef"
        :model="queryParams"
        class="mb20"
        layout="inline"
    >
      <t-form-item label="名称" prop="name">
        <t-input
            v-model="queryParams.name"
            clearable
            placeholder="功能名称"
            style="width: 200px"
            @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search"/>
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh"/>
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
            theme="primary"
            @click="handleAdd"
            v-hasPermi="['Nbot:menu:add']"
        >新增
          <template #icon>
            <t-icon name="add"/>
          </template>
        </t-button>
      </t-col>
      <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
        stripe
        v-loading="loading"
        :columns="columns"
        :data="list"
        row-key="id"
    >
      <template #icon="scope">
        <t-image
            :src="scope.row.icon"
            fit="contain"
            :style="{ width: '120px', height: '120px' }"
        />
      </template>
      <template #action="scope">
        <t-button
            theme="primary"
            variant="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Nbot:menu:edit']"
        >修改
          <template #icon>
            <t-icon name="edit"/>
          </template>
        </t-button>
        <t-button
            theme="primary"
            variant="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Nbot:menu:remove']"
        >删除
          <template #icon>
            <t-icon name="delete"/>
          </template>
        </t-button>
      </template>
    </t-table>

    <pagination
        v-show="total > 0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <t-dialog v-model:visible="open" :header="title" attach="body" width="780px">
      <t-form ref="menuRef" :data="form" :rules="rules" label-width="80px">
        <t-form-item label="功能名称" prop="name" requiredMark>
          <t-input
              v-model="form.name"
              placeholder="请输入功能名称"
          />
        </t-form-item>
        <t-form-item requiredMark label="功能图标" prop="icon">
          <t-upload
              ref="uploadRef"
              action="https://admin.yaojunzhiku.com/prod-api/common/uploadOSS"
              v-model="img"
              sizeLimit="1024KB"
              :max="1"
              theme="image"
              :onSuccess="onSuccess"
              tips="请选择图片文件上传,文件大小不超过1024KB"
              accept="image/*"
          >
          </t-upload>
        </t-form-item>
        <t-form-item label="功能介绍" prop="desc" requiredMark>
          <t-input
              v-model="form.desc"
              placeholder="请输入功能介绍"
          />
        </t-form-item>
        <t-form-item label="跳转路径" prop="path" requiredMark>
          <t-input
              v-model="form.path"
              placeholder="请输入跳转路径"
          />
        </t-form-item>
        <t-form-item label="是否隐藏" prop="hide">
          <t-switch v-model="form.hideChecked"></t-switch>
        </t-form-item>
        <t-form-item label="排序" prop="sort">
          <t-input
              v-model="form.sort"
              placeholder="请输入排序"
              type="number"
          />
        </t-form-item>
      </t-form>
      <template #footer>
        <div class="dialog-footer">
          <t-button theme="primary" @click="submitForm">确 定</t-button>
          <t-button theme="default" @click="cancel">取 消</t-button>
        </div>
      </template>
    </t-dialog>
  </div>
</template>

<script setup>
import {
  addMenu, delMenu,
  getMenu,
  listMenu, updateMenu,
} from "@/api/nbot/menu";
import {reactive, ref, toRefs, getCurrentInstance} from "vue";

const {proxy} = getCurrentInstance();


const list = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const img = ref([]);

const columns = ref([
  {colKey: "icon", title: "功能图标", width: 100},
  {colKey: "name", title: "功能名称", width: 100},
  {colKey: "desc", title: "功能介绍", width: 100},
  {colKey: "path", title: "跳转路径", width: 100},
  {colKey: "sort", title: "排序", width: 100},
  {colKey: "hide", title: "APP端隐藏", width: 100},
  {colKey: "action", title: "操作", width: 100},
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  },
  rules: {
    name: [
      {required: true, message: "功能名称不能为空", trigger: "blur"},
    ],
    icon: [
      {required: true, message: "功能图标不能为空", trigger: "blur"},
    ],
    path: [
      {required: true, message: "跳转路径不能为空", trigger: "blur"},
    ]
  },
});

const {queryParams, form, rules} = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listMenu(queryParams.value).then((response) => {
    list.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  img.value = []
  form.value = {
    name: undefined,
    icon: undefined,
    desc: undefined,
    path: undefined,
    sort: 9,
    hideChecked: false,
    hide: 0,
  };
  proxy.resetForm("menuRef");
}

const onSuccess = ({response}) => {
  form.value.icon = response.url;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加功能菜单";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getMenu(id).then((response) => {
    form.value = response.data;
    img.value = [{
      url: form.value.icon
    }]
    open.value = true;
    title.value = "修改功能菜单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["menuRef"].validate().then((valid) => {
    if (valid) {
      form.value.hide = form.value.hideChecked ? 1 : 0
      if (form.value.id !== undefined) {
        updateMenu(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMenu(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal
      .confirm('是否确认删除编号为"' + id + '"的数据项？')
      .then(function () {
        return delMenu(id);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}

getList();
</script>
